package com.dao;

import com.utils.JdbcUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class TeacherDao {
    
    public List<Map<String, Object>> getAllTeachers() throws SQLException {
        String sql = "SELECT t.*, c.cname FROM Teacher t LEFT JOIN Company c ON t.tcompany = c.cno ORDER BY t.tno";
        List<Map<String, Object>> teachers = new ArrayList<>();
        
        try (Connection conn = JdbcUtils.getDs().getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            
            while (rs.next()) {
                Map<String, Object> teacher = new HashMap<>();
                teacher.put("tno", rs.getInt("tno"));
                teacher.put("name", rs.getString("tname"));
                teacher.put("sex", rs.getInt("tsex"));
                teacher.put("mobile", rs.getString("tmobile"));
                teacher.put("company", rs.getString("cname"));
                teacher.put("companyId", rs.getInt("tcompany"));
                teachers.add(teacher);
            }
        }
        return teachers;
    }
    
    public void addTeacher(Map<String, Object> teacher) throws SQLException {
        String sql = "INSERT INTO Teacher (tno, tname, tsex, tmobile, tcompany) VALUES (?, ?, ?, ?, ?)";
        
        try (Connection conn = JdbcUtils.getDs().getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, (Integer) teacher.get("tno"));
            pstmt.setString(2, (String) teacher.get("name"));
            pstmt.setInt(3, (Integer) teacher.get("sex"));
            pstmt.setString(4, (String) teacher.get("mobile"));
            pstmt.setInt(5, (Integer) teacher.get("companyId"));
            
            pstmt.executeUpdate();
        }
    }
    
    public void updateTeacher(Map<String, Object> teacher) throws SQLException {
        String sql = "UPDATE Teacher SET tname = ?, tsex = ?, tmobile = ?, tcompany = ? WHERE tno = ?";
        
        try (Connection conn = JdbcUtils.getDs().getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, (String) teacher.get("name"));
            pstmt.setInt(2, (Integer) teacher.get("sex"));
            pstmt.setString(3, (String) teacher.get("mobile"));
            pstmt.setInt(4, (Integer) teacher.get("companyId"));
            pstmt.setInt(5, (Integer) teacher.get("tno"));
            
            pstmt.executeUpdate();
        }
    }
    
    public void deleteTeacher(int tno) throws SQLException {
        String sql = "DELETE FROM Teacher WHERE tno = ?";
        
        try (Connection conn = JdbcUtils.getDs().getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, tno);
            pstmt.executeUpdate();
        }
    }
} 